package cn.stylefeng.guns.sys.core.util;

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicLong;

public class OrderCodeMaker {

	private static AtomicLong SN = new AtomicLong(0);
	
	// 生成固定长度的单服务器不会重复的订单号，前缀长度最大为6
	public static String genOrderNumber(String prefix) {
		// 13位当前时间戳
		long timeMills = System.currentTimeMillis();
		// 6位验证码
		int validCode = 100000 + ThreadLocalRandom.current().nextInt(900000);
		// 生成6位循环使用的顺序号
		long next = SN.getAndIncrement();
		//序号范围：[262,144, 524,287]
		int no = 262144 |((int)next & 262143);
		StringBuilder sb = new StringBuilder();
		if(prefix != null) {
			sb.append(prefix);
		}
		sb.append(timeMills);
		sb.append(no);
		sb.append(validCode);
		return sb.toString();
	}
}
